<?php
namespace app\index\model;

use think\Model;

class Label extends Model
{
    //方法定义关联
    public function article(){
        return $this->belongsToMany('Article', 'ArticleLabel');
    }

    //获取器和修改器

    /**
     * 将color转为16进制字符串
     * @param $value
     * @return string
     */
    public function getColorAttr($value){
        return sprintf('%06s', dechex($value));
    }

    public function setColorAttr($value){
        return hexdec($value);
    }

    /**
     * 获取列表
     * @param array $where
     * @param array $whereAssoc
     * @return array|\PDOStatement|string|\think\Collection
     */
    public function getDetail($where=[], $whereAssoc=[]){
        $object = $this->where($where);
        //若关联条件存在
        foreach ($whereAssoc as $key => $value){
            $object = $object->hasWhere($key, $value);
        }
        return $object->select();
    }

}